<?php
 require_once(dirname(__FILE__) . '/../../inc/Database.inc');
 require_once(dirname(__FILE__) . '/../../fckeditor/fckeditor.php');
	
 	$link = ConnectToDatabase2();
 
	 if( $_POST['NotFirstTime'] == 'True')
	 {	 	
	  	$insDate = mysql_real_escape_string($_POST['date'] , $link);
	 	$insTitle= mysql_real_escape_string(htmlentities($_POST['title'], ENT_QUOTES));
	 	$insNews = mysql_real_escape_string(htmlentities($_POST['news'], ENT_QUOTES));
	 	$insId   = mysql_real_escape_string($_POST['id']   , $link);
	 	
	 	if($_POST["action"] == "Submit")
	 	{
		 	if(KeyExists("news","id",$insId,$link))
		 	{
		 		//we need to update the table here
		 		$sql = "UPDATE `news` SET `title`='" . $insTitle . "', `news`='" . $insNews . "' WHERE `id`='" . $insId . "'";
		 		RunSql($sql,$link);
		 		echo "Updated Successfully!";
		 	}
		 	else
		 	{
			 	$sql = "INSERT INTO news (`date`, `news`, `title`) VALUES('$insDate','$insNews','$insTitle');";
			 	RunSql($sql,$link);
			 	echo "Inserted Successfully!";
		 	}
	 	}
	 	else
	 	{
	 		$sql = "DELETE FROM `news` WHERE `id`='" . $insId . "'";
	 		RunSql($sql,$link);
	 		echo "Deleted Successfully";
	 	}
	 }

	 	$sql = "SELECT * FROM `news` ORDER BY `date` ASC";
	 	$result = RunSql($sql,$link);
	 	$i = 1;
	 	while($row = mysql_fetch_assoc($result))
	 	{
	 		$date[$i]  = $row['date'];
	 		$title[$i] = html_entity_decode(stripslashes($row['title']), ENT_QUOTES);
	 		$news[$i]  = html_entity_decode(stripslashes($row['news'] ), ENT_QUOTES);
	 		$id[$i]    = $row['id'];
	 		$i++;
	 	}

	 	mysql_free_result($result);
	 	mysql_close($link);
	 	
	 	$newTitle = "New...";
	 	$date[$i]  = date("Y-m-d\ H:i:s");
	 	$title[$i] = $newTitle;
	 	$news[$i]  = "";
	 	$id[$i]    = "";
?>
<script type="text/javascript" src="<?php echo dirname(__file__)?>\..\..\fckeditor\fckeditor.js"></script>

<form method="post" >
	<input type="hidden" name="NotFirstTime" value="True" />
	<input type="hidden" name="date" value="" id="date" />
	<input type="hidden" name="id" value="" id="id" />

	<label for="titleCombo"> News Entry to Edit </label>
	<br />
	<select name = "titleCombo" style="width:323" OnChange="onTitleComboChange()" id="titleCombo">
	<?php
		
	 	for ($i = count($title); $i > 0 ; $i--)
	 	{
    		echo '<option value="' . $i . '">' . $title[$i] . ' (' . $date[$i] . ')</option>';
		}
	?>
	</select>

	<br />
	<br />

	<label for="title"> Title </label> 	
	<br / >
	<input type="text" size="50" name="title" value="" id="title"/>
	<br />
	<br />		
	<label for="body"> News </label>
			<?php 
				$oFCKeditor = new FCKeditor('news') ;
				$oFCKeditor->BasePath = '../fckeditor/';
				$oFCKeditor->ToolbarSet = 'Default'; //MainHeader
				$oFCKeditor->Height = '300';
				$oFCKeditor->Width = '780';
				$oFCKeditor->Value = '';
			 	$oFCKeditor->Create();
				  ?>

				<input type="Submit" name="action" value="Delete" />
				<input type="Submit" name="action" value="Submit" />
				
</form>

<script type="text/javascript">
	<?php 
	echo 'var newsInfo = [';
	$firstTime = true;
	foreach ($date as $i => $value) {
		if (!$firstTime)
		{
			echo ',';
		}
		
		str_ireplace("%body%", "black", "<body text=%BODY%>");
		
		$formattedNews = addslashes(str_ireplace("\r\n","",$news[$i])); 
		$formattedTitle = addslashes(str_ireplace("\r\n","",$title[$i]));
		
    	echo '{' . 
    	 		'"date":  "' . $date[$i]  . '",' . 
    			'"news":  "' . $formattedNews . '",' .
    			'"title": "' . $formattedTitle . '",' .
    			'"id":    "' . $id[$i]    . '" ' .
			 '}';
    	$firstTime = false;
	}
	echo '];';
	
	?>

	
	
	function onLoad()
	{
		onTitleComboChange()
	}
	
	function onTitleComboChange()
	{
		var value = document.getElementById('titleCombo').value
		value--;
		
		var title = document.getElementById('title')
		title.value = newsInfo[value].title

		if(title.value == "<?php echo $newTitle?>")
		{
			title.value = ""
		}
		

		var date = document.getElementById('date')
		date.value = newsInfo[value].date
		
		var oEditor = FCKeditorAPI.GetInstance('news') ;
		oEditor.SetData( newsInfo[value].news ) ;

		var id = document.getElementById('id')
		id.value = newsInfo[value].id
	}

</script>

